//
//  AMUserInfoViewController.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 01/07/16.
//  Copyright (c) 2016 WeiDu Energy Tech. All rights reserved.
//

#import "HOUserInfoViewController.h"
#import "HOTextField.h"
#import "HOEditInfoViewController.h"
#import "HOConstants.h"
#import "HOUserInfoDataModule.h"
#import "HOAreaSelectionViewController.h"
#import "Logger.h"
#import "HOUIMgr.h"
#import "HOUtils.h"
#import "HOStaticDataMgr.h"

@implementation HOUserInfoViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self initTableView];
	textFields = [[NSMutableArray alloc]init];
	self.userInfo = [[HOStaticDataMgr sharedInstance] getUserInfo];;
//    LogInf(@"self.userInfo:%@", self.userInfo);
    [self startRequest];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = YES;
    [self setNavigationItemTitle:NSLocalizedString(@"个人信息", @"个人信息")];
    [super viewWillAppear:animated];
    [baseTableview reloadData];
//    self.province = self.userInfo.province;
//    self.city = self.userInfo.city;
//    self.area = self.userInfo.district;
}

- (void)startRequest
{
    [controller sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetUser}
                          withModules:@[[self numberWithInt:moduleType]]];
}
- (void)dealloc
{
    [textFields release];
    [cityAddress release];
    [super dealloc];
}

#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0 || section == 1)
        return 3;
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];

    
//    UIView *lineView = (UIView *)[cell viewWithTag:111];
//    if(lineView == nil)
//    {
//        lineView = [[UIView alloc]
//                    initWithFrame:CGRectMake(0,
//                                             cellHeight - 1,
//                                             tableView.frame.size.width,
//                                             1)];
//        [lineView setBackgroundColor:MAIN_COLOR];
//        lineView.tag = 111;
//        [cell.contentView addSubview:lineView];
//        [lineView release];
//    }
//    lineView = (UIView *)[cell viewWithTag:111];
//    lineView.hidden = YES;
    
    UILabel *lbl = (UILabel *)[cell viewWithTag:11];
    if(lbl == nil)
    {
        lbl = [[UILabel alloc] initWithFrame:
           CGRectMake(HO_HORIZONAL_PADDING,
                      (cellHeight - 22) / 2,
                      200,
                      22)];
        lbl.text = NSLocalizedString(@"《神鸥服务协议》", @"《神鸥服务协议》");
        lbl.font = [UIFont systemFontOfSize:CELL_FONT_SIZE];
        lbl.textColor = UIColorFromRGB(0x333333);
        lbl.tag = 11;
        [cell.contentView addSubview:lbl];
        [lbl release];
    }
    lbl = (UILabel *)[cell viewWithTag:11];
    
    
    UILabel *lbl1 = (UILabel *)[cell viewWithTag:44];
    if(lbl1 == nil)
    {
        lbl1 = [[UILabel alloc] initWithFrame:
                CGRectMake(80,
                           (cellHeight - 22) / 2,
                           tableView.frame.size.width - 80 - HO_HORIZONAL_PADDING - 20,
						   22)];
		lbl.text = NSLocalizedString(@"《神鸥服务协议》", @"《神鸥服务协议》");
        lbl1.font = [UIFont systemFontOfSize:CELL_FONT_SIZE];
        lbl1.textAlignment = NSTextAlignmentRight;
        lbl1.textColor = UIColorFromRGB(0x666000);
        lbl1.tag = 44;
        [cell.contentView addSubview:lbl1];
        [lbl1 release];
    }
    lbl1 = (UILabel *)[cell viewWithTag:44];
    lbl1.hidden = YES;
    HOTextField *inputBox = (HOTextField *)[cell viewWithTag:22];
    if(!(indexPath.section == 0 && indexPath.row == 2))
    {
        if(inputBox == nil)
        {
            inputBox = [[HOTextField alloc] initWithFrame:
                        CGRectMake(80,
                                   (cellHeight - 22) / 2,
                                   tableView.frame.size.width - 80 - HO_HORIZONAL_PADDING - 20,
                                   22)];
            inputBox.placeholder = NSLocalizedString(@"未填写", @"未填写");
            inputBox.font = [UIFont systemFontOfSize:CELL_FONT_SIZE];
            inputBox.tag = 22;
            [inputBox setEnabled:NO];
            inputBox.textAlignment = NSTextAlignmentRight;
            inputBox.textColor = UIColorFromRGB(0x666000);
            [cell.contentView addSubview:inputBox];
            [inputBox release];
        }
        inputBox = (HOTextField *)[cell viewWithTag:22];
        inputBox.indexPath = indexPath;
    }
    
    if(indexPath.section == 0 && indexPath.row == 0)
    {
        lbl.text = NSLocalizedString(@"昵称", @"昵称");
        if(self.userInfo.name == nil && self.userInfo.name.length == 0)
        {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle = UITableViewCellSelectionStyleGray;
        }
        else
        {
            
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        inputBox.text = self.userInfo.name;
        
    }
    else if(indexPath.section == 0 && indexPath.row == 1)
    {
        lbl.text = NSLocalizedString(@"姓名", @"姓名");
        //        lineView.hidden = NO;
        
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        inputBox.text = self.userInfo.name;
    }
    else if(indexPath.section == 0 && indexPath.row == 2)
    {
        lbl.text = NSLocalizedString(@"ID", @"ID");
        
        HOTextField *inputBox = (HOTextField *)[cell viewWithTag:22];
        if(inputBox)
            inputBox.hidden = YES;
        lbl1.frame = CGRectMake(80,
                   (cellHeight - 22) / 2,
                   tableView.frame.size.width - 80 - HO_HORIZONAL_PADDING - 20,
                                22);
        lbl1.hidden = NO;
        lbl1.textAlignment = NSTextAlignmentRight;
        lbl1.text = [NSString stringWithFormat:@"%d", self.userInfo.userId];
        lbl1.textColor = UIColorFromRGB(0x666000);
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    else if(indexPath.section == 1 && indexPath.row == 0)
    {
        lbl.text = NSLocalizedString(@"省市区", @"省市区");
//        lineView.hidden = NO;
        
//        inputBox.text = [NSString stringWithFormat:@"%@ %@ %@",
//                         self.userInfo.province == nil ? @"" : self.userInfo.province,
//                         self.userInfo.city == nil ? @"" : self.userInfo.city,
//                         self.userInfo.district== nil ? @"" : self.userInfo.cit];
    }
    else if(indexPath.section == 1 && indexPath.row == 1)
    {
        lbl.text = NSLocalizedString(@"地区", @"地区");
        lbl.numberOfLines = 2;
        lbl.lineBreakMode = NSLineBreakByWordWrapping;
//        lineView.hidden = NO;
        inputBox.text = self.userInfo.city;
    }
    else if(indexPath.section == 1 && indexPath.row == 2)
    {
        lbl.text = NSLocalizedString(@"邮政编码", @"邮政编码");
//        inputBox.text = self.userInfo.postCode;
    }
    else if(indexPath.section == 2 && indexPath.row == 0)
    {
        lbl.text = NSLocalizedString(@"联系电话", @"联系电话");
//        lineView.hidden = NO;
        
        inputBox.text = self.userInfo.telPhone;
    }
    else if(indexPath.section == 2 && indexPath.row == 1)
    {
        lbl.text = @"QQ";
//        inputBox.text = self.userInfo.qq;
    }
    return cell;
}
#pragma mark
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0 && indexPath.row == 2)
    {
        return;
    }
    if(indexPath.section == 1 && indexPath.row == 0)
    {
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
        HOAreaSelectionViewController *viewCtrl = [[[HOAreaSelectionViewController alloc]initWithModule:moduleType withSelType:HO_AREA_SELECTION_PROVINCE] autorelease];
        [self.navigationController pushViewController:viewCtrl animated:YES];
//        [KKAreaPicker showPickerWithTitle:@"省市区选择" pickerType:KKAreaPickerTypeProviceCityArea defaultValue:cityAddress onCancel:^(KKAreaPicker *picker) {
//            ;
//        } onCommit:^(KKAreaPicker *picker, KKAdrress *address) {
//            cityStr = [NSString stringWithFormat:@"选中:%@，%@，%@",
//                       cityAddress.provice,
//                       cityAddress.city,
//                       cityAddress.area];
//            [baseTableview reloadData];
//        }];
        return;
    }
    HOEditInfoViewController *viewCtl = [[[HOEditInfoViewController alloc]initWithModule:HO_MODULE_TYPE_USER] autorelease];
    
    if(indexPath.section == 0 && indexPath.row == 0)
    {
        if(self.userInfo.name == nil || self.userInfo.name.length == 0)
        {
            viewCtl.limitedCount = 15;
            viewCtl.navItemTitle = NSLocalizedString(@"姓名", @"姓名");
            viewCtl.fieldName = kHO_FIELD_NickName;
            viewCtl.placeHolder = NSLocalizedString(@"请输入", @"请输入");
            viewCtl.needHideLimitedLbl = YES;
            viewCtl.oldValue = self.userInfo.name;
        }
        else
        {
            return;
        }
    }
    else if(indexPath.section == 0 && indexPath.row == 1)
    {
        return;
//        viewCtl.limitedCount = 15;
//        viewCtl.navItemTitle = NSLocalizedString(@"姓名", @"姓名");
//        viewCtl.placeHolder = NSLocalizedString(@"请输入", @"请输入");
//        viewCtl.needHideLimitedLbl = YES;
//        viewCtl.oldValue = self.userInfo.name;
    }
    else if(indexPath.section == 1 && indexPath.row == 1)
    {
        viewCtl.limitedCount = 44;
        viewCtl.navItemTitle = NSLocalizedString(@"地区", @"地区");
        viewCtl.placeHolder = NSLocalizedString(@"请输入", @"请输入");
        viewCtl.fieldName = kHO_FIELD_Address;
        viewCtl.oldValue = self.userInfo.city;
        viewCtl.inputBoxHeight = 140;
    }
    else if(indexPath.section == 1 && indexPath.row == 2)
    {
        viewCtl.limitedCount = 6;
        viewCtl.navItemTitle = NSLocalizedString(@"邮政编码", @"邮政编码");
        viewCtl.placeHolder = NSLocalizedString(@"请输入", @"请输入");
        viewCtl.isOneLine = YES;
        viewCtl.needHideLimitedLbl = YES;
        viewCtl.fieldName = kHO_FIELD_PostCode;
//        viewCtl.oldValue = self.userInfo.postCode;
        viewCtl.keypadType = UIKeyboardTypeNumberPad;
    }
    else if(indexPath.section == 2 && indexPath.row == 0)
    {
        viewCtl.limitedCount = 13;
        viewCtl.isOneLine = YES;
        viewCtl.navItemTitle = NSLocalizedString(@"联系电话", @"联系电话");
        viewCtl.placeHolder = NSLocalizedString(@"请输入", @"请输入");
        viewCtl.needHideLimitedLbl = YES;
        viewCtl.fieldName = kHO_FIELD_TelPhone;
        viewCtl.oldValue = self.userInfo.telPhone;
//        viewCtl.keypadType = UIKeyboardtypep;
    }
    [self.navigationController pushViewController:viewCtl animated:YES];
}

#pragma mark

- (void)didReceiveCommand:(NSString *)action data:(id)data
{
	[super didReceiveCommand:action data:data];
    if([action isEqualToString:kHO_CMD_SELECDTED_Province])
    {
        self.province = data;
    }
   }

- (void)didReceiveResponse:(HOBaseResponseEntity *)response
{
    if(![response.action isEqualToString:kHO_ACTION_GetUser])
        return;
	[super didReceiveResponse:response];
	if([response isRequestDone])
    {
        [baseTableview reloadData];
        
    }
}

@end
